import java.util.ArrayList;
import java.util.Stack;

public class Solution {
}

// 利用 栈 本身结构的特性实现 倒序
class Solution1 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        Stack<Integer> st = new Stack<>();
        while (listNode != null) {
            st.push(listNode.val);
            listNode = listNode.next;
        }
        while (!st.empty()) {
            list.add(st.pop());
        }
        return list;
    }
}

// 利用函数的 递归 实现 倒序
class Solution2 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    public void printListFromTailToHeadHelper(ArrayList<Integer> list, ListNode listNode) {
        if (listNode == null) {
            return;
        }
        printListFromTailToHeadHelper(list, listNode.next);
        list.add(listNode.val);
        return;
    }

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        printListFromTailToHeadHelper(list, listNode);
        return list;
    }
}